<template>
  <div class="todo-footer">
    <label>
      <!--<input type="checkbox" :checked="checkAll" @change="checkAllOrNone"/>-->
      <input type="checkbox" v-model="checkAll"
             :disabled="disabled" />
    </label>
    <span>
            <span>
              <span>已完成</span>
              <strong style="color: red">{{computedCount}}</strong>
            </span>
            <span> / </span>
            <span>
              <span>全部</span>
              <strong style="color: green">{{this.list.length}}</strong>
            </span>
    </span>
    <button class="btn btn-danger" @click="clearTask">清除已完成任务</button>
  </div>
</template>

<script>
    export default {
        name: "todo-footer",
        props:{list:Array},
        /*data(){
          return {
            disabled:false
          }
        },*/
        methods:{
          //清空已完成任务
          clearTask(){this.$bus.$emit("clearTask")},
          //全选&全不选
          //checkAllOrNone(ev){this.$bus.$emit("checkAllOrNone",ev.target.checked)}
        },
        computed:{
          //已经完成的待办任务的数量
          computedCount(){
            let count = 0;
            this.list.forEach(task=>{
              if(task.checked === true){
                count++
              }
            })
            return count;

            /*return this.list.reduce((adder,task)=>{
              return task.checked ? adder+1 : adder;
            },0)*/
          },
          //全选按钮是否要选中
          checkAll:{
            get(){
              // 1. 全部数量 等于 已完成数量时 全选按钮该选中
              // 2. 当 全部数量 等于 已完成数量 等于0时 全选按钮不该选中
              return (
                (this.list.length  === this.computedCount)
                &&
                !(this.list.length===0&&this.computedCount===0)
              )
            },
            set(newVal){
              this.$bus.$emit("checkAllOrNone",newVal)
            }
          },
          //代表全选按钮是否要禁用
          disabled(){
            return this.list.length === 0
          }
        }
    }
</script>

<style scoped>
  /*damu add start*/
  input[disabled] {cursor: not-allowed;}
  /*damu add end*/

  .todo-footer {
    height: 40px;
    line-height: 40px;
    padding-left: 6px;
    margin-top: 5px;
  }

  .todo-footer label {
    display: inline-block;
    margin-right: 20px;
    cursor: pointer;
  }

  .todo-footer label input {
    position: relative;
    top: -1px;
    vertical-align: middle;
    margin-right: 5px;
  }

  .todo-footer button {
    float: right;
    margin-top: 5px;
  }
</style>
